package com.ziyia.contactstest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EventListener;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Activity T = MainActivity.this;
    private Button readphone, readsms, sendsms;
    private ListView listView;

    private List<String> list = new ArrayList<>();
    private ArrayAdapter<String> arrayAdapter;

    private void init() {
        readphone = findViewById(R.id.readphone);
        readsms = findViewById(R.id.readsms);
        sendsms = findViewById(R.id.sendsms);
        listView = findViewById(R.id.listView);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();


        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);


            readsms.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ContextCompat.checkSelfPermission(T, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(T, new String[]{Manifest.permission.READ_CONTACTS}, 1);
                    } else {
                        list.clear();
                        readContactsSms();
                    }
                }
            });


            readphone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ContextCompat.checkSelfPermission(T, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(T, new String[]{Manifest.permission.READ_SMS}, 2);
                    } else {
                        list.clear();
                        readContacts ();
                    }
                }
            });

            sendsms.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ContextCompat.checkSelfPermission(T, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(T, new String[]{Manifest.permission.SEND_SMS}, 3);
                    } else {
                        sendSms();
                    }
                }


            });


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull @org.jetbrains.annotations.NotNull String[] permissions, @NonNull @org.jetbrains.annotations.NotNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 1:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContacts();
                } else {
                    Toast.makeText(T, "没有权限读取电话", Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContactsSms();
                } else {
                    Toast.makeText(T, "没有权限读取短信", Toast.LENGTH_SHORT).show();
                }
                break;
            case 3:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    sendSms();
                } else {
                    Toast.makeText(T , "没有权限发送短信", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }
    private void sendSms() {
        String content = "cxye";
        String phone = "10086";
        if (!content.isEmpty() && !phone.isEmpty()) {
            SmsManager aDefault = SmsManager.getDefault();
            ArrayList<String> strings = aDefault.divideMessage(content);
                aDefault.sendTextMessage(phone, null , content, null, null);
            Toast.makeText(T, "短信发送成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(T, "发送内容为空", Toast.LENGTH_SHORT).show();
        }
    }
    private void readContacts() {
        ContentResolver contentResolver = getContentResolver();
        Cursor query = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while (query.moveToNext()) {
            // 获取联系人姓名
            String displayName = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            // 获取联系人手机号
            String number = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            list.add("姓名 = " + displayName + "，手机号 = " + number);
        }
        arrayAdapter.notifyDataSetChanged();
        query.close();
    }

    private void readContactsSms() {
        ContentResolver contentResolver = getContentResolver();
        Cursor query = contentResolver.query(Uri.parse("content://sms/"), new String[]{"_id", "address", "type", "body", "date"}, null, null, null);



        while (query.moveToNext()) {
            int id = query.getInt(0);
            String address = query.getString(1);
            int type = query.getType(2);
            String body = query.getString(3);
            long date = query.getLong(4);
            // 获取联系人姓名
            Sms sms = new Sms(id, address, type, body, date);
            list.add(sms.toString());
        }
        arrayAdapter.notifyDataSetChanged();
        query.close();
    }
}